﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Health;

namespace SPHealth.SharePoint.HealthRules.SP.Rules.Configuration
{
    public class InfoNoAlertForProblemsAndSolutionsList : SPHealthAnalysisRule
    {
        #region Variables
        #endregion

        #region Rule Setup

        // the category to display this rule under
        public override SPHealthCategory Category
        {
            get { return SPHealthCategory.Configuration; }
        }

        // the type or error to flag this rule as
        public override SPHealthCheckErrorLevel ErrorLevel
        {
            get { return SPHealthCheckErrorLevel.Information; }
        }

        // the Explanation for the rule and what is not working
        public override string Explanation
        {
            get { return "To ensure that system administrators are aware of potential issues, it is recommended that an alert is assigned to the Review Problems and Solutions list in Central Administration."; }
        }

        // how to fix the issue
        public override string Remedy
        {
            get { return "Assign an alert to the Review Problems and Solutions list in Central Administration to ensure that system administrators are notified in a timely manner when new issues are identified within the farm."; }
        }

        // the Summary for the rule
        public override string Summary
        {
            get { return "Ensure the Review Problems and Solutions list in Central Administration has at least one alert assigned to it."; }
        }
        #endregion

        #region Methods

        public override SPHealthAnalysisRuleAutomaticExecutionParameters AutomaticExecutionParameters
        {
            get
            {
                SPHealthAnalysisRuleAutomaticExecutionParameters retval = new SPHealthAnalysisRuleAutomaticExecutionParameters();
                retval.Schedule = SPHealthCheckSchedule.Daily;
                retval.Scope = SPHealthCheckScope.Any;
                retval.ServiceType = typeof(SPTimerService);
                return retval;
            }
        }

        // this method is called whenever the rule is executed
        public override SPHealthCheckStatus Check()
        {
            if (!SPFarm.Joined)
            {
                throw new InvalidOperationException();
            }

            // Get the Central Admin site.
            using (SPWeb centralAdminWeb = common.GetCentralAdminWeb())
            {
                if (centralAdminWeb != null)
                {
                    SPList HealthReportsList = centralAdminWeb.GetList("Lists/HealthReports");
                    foreach (SPAlert alert in centralAdminWeb.Alerts)
                    {
                        // check if any of the alerts are for the health reports list
                        if (alert.ListID == HealthReportsList.ID)
                        {
                            // this alert is assigned to the health reports list.
                            return SPHealthCheckStatus.Passed;
                        }
                    }
                }
            }
            // no alerts were found
            return SPHealthCheckStatus.Failed;
        }

        #endregion

    }
}
